@extends('layout.master')

@section('title','管理员列表')

@section('css')
    @parent
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
    <style>
        .floating-labels .radioInput label {
            position: static;
        }

        .floating-labels button.form-control {
            padding: .8rem 3.2rem .8rem 1.2rem;
        }

        .bootstrap-select span.filter-option {
            font-size: 14px;
        }

        .dropify-wrapper {
            border: 0;
        }
    </style>
@endsection
@section('main')

    <!-- form itself -->
    <form id="test-form" class="mfp-hide white-popup-block floating-labels" method="post">
        <h3>添加管理员</h3>
        <fieldset style="border:0;">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group m-b-25 m-t-10">
                        <input type="text" class="form-control" id="input1" name="name" required><span
                                class="highlight"></span> <span class="bar"></span>
                        <label for="input1">用户名</label>
                    </div>
                </div>
            </div>

            <div class="form-group m-b-40 m-t-10">
                <input type="password" class="form-control" name="password" id="input4" required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="input4">安全密码</label>
            </div>
            <div class="form-actions">
                <div class="row">
                    <div class="col-md-12">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-9">
                                <button type="submit" class="btn btn-success" id="add-admin"><i
                                            class="fa fa-check"></i> 保存
                                </button>
                                {!! csrf_field() !!}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
    </form>
    <div class="white-box">
        <div class="row">
            <div class="panel-action col-md-1" style="margin-top: -9px">
                <a class="popup-with-form btn fcbtn btn btn-outline btn-info btn-1c" href="#test-form">
                    <span>添加管理员</span>
                </a>
            </div>
        </div>

        <hr>

        <div class="row">
            @foreach(App\Admin::all() as $value)
                <div class="col-md-4 col-sm-4">
                    <div class="white-box">
                        <div class="row">
                            <div class="col-md-4 col-sm-4 text-center">
                                <a href="/system/admin/detail/{{ $value->id }}">
                                    <img src="{{ asset('plugins/images/users/genu.jpg') }}" alt="name"
                                         class="img-circle img-responsive">
                                </a>
                            </div>
                            <div class="col-md-6 col-sm-6">
                                <h3 class="box-title m-b-0">{{ $value->name }}</h3>
                                @if(DB::table('role_user')->where('user_id', $value->id)->value('role_id'))
                                    <small>{{ App\Role::findOrFail(DB::table('role_user')->where('user_id', $value->id)->value('role_id'))->display_name }}</small>
                                @endif
                            </div>
                            @if ($value->id !== Auth::id())
                            <div class="col-md-2 col-sm-2 pull-right">
                                <button type="button" data-id="{{ $value->id }}"
                                        class="btn btn-primary btn-circle waves-effect delete"><i
                                            class="fa fa-times"></i></button>
                            </div>
                            @endif
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    </div>
@endsection
@section('js')
    @parent
    <!-- Magnific popup JavaScript -->
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>
    <!-- icheck -->
    <script src="{{ asset('plugins/bower_components/icheck/icheck.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/icheck/icheck.init.js') }}"></script>
    <script src="{{ asset('js/jasny-bootstrap.js') }}"></script>
    <!-- jQuery file upload -->
    <script src="{{ asset('plugins/bower_components/dropify/dist/js/dropify.min.js') }}"></script>
    <script>
        $(document).ready(function () {
            // Basic
            $('.dropify').dropify();
        });
    </script>
    <script src="{{ asset('plugins/bower_components/bootstrap-select/bootstrap-select.min.js') }}"
            type="text/javascript"></script>
    <!-- bt-switch -->
    <script src="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.js') }}"></script>

    <!-- Magnific popup JavaScript -->
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>
    <script type="text/javascript">
        $(".bt-switch input[type='checkbox'], .bt-switch input[type='radio']").bootstrapSwitch();
        var radioswitch = function () {
            var bt = function () {
                $(".radio-switch").on("switch-change", function () {
                    $(".radio-switch").bootstrapSwitch("toggleRadioState")
                }),
                    $(".radio-switch").on("switch-change", function () {
                        $(".radio-switch").bootstrapSwitch("toggleRadioStateAllowUncheck")
                    }),
                    $(".radio-switch").on("switch-change", function () {
                        $(".radio-switch").bootstrapSwitch("toggleRadioStateAllowUncheck", !1)
                    })
            };
            return {
                init: function () {
                    bt()
                }
            }
        }();
        $(document).ready(function () {
            radioswitch.init()
        });


        $(document).ready(function () {
            $('#test-form').submit(function (e) {
                e.preventDefault();
                swal({
                        title: "",
                        text: "确定添加么？",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "是的",
                        closeOnConfirm: false
                    },
                    function () {
                        $.ajax({
                            type: "POST",
                            url: '/system/adminAdd',
                            data: $('#test-form').serialize(),// 你的formid
                            async: false,
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                            error: function (e) {
                                if (e.readyState === 4 && e.status === 422) {
                                    for (var i in e.responseJSON) {
                                        if (e.responseJSON[i] !== 'The given data was invalid.') {
                                            err = e.responseJSON[i];
                                            for (var j in err) {
                                                toasterror(err[j][0]);
                                            }
                                        }
                                    }
                                }
                            },
                            success: function (data) {
                                if (data.status === 1) {
                                    swal(data.message, '', 'success');
                                    $('.confirm').click(function () {
                                        location.reload();
                                    });
                                } else {
                                    swal(data.message);
                                }
                            }
                        });
                    })
            });
        });

        $('.delete').click(function (e) {
//            e.preventDefault();
            var id = $(this).data('id');
            swal({
                    title: "",
                    text: "确定删除么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: '/system/admin/del/' + id,
                        type: 'POST',
                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message);
                            } else {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            }
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        }
                    })
                }
            );
        });
    </script>

@endsection